import { createRouter, createWebHistory,createWebHashHistory, RouteRecordRaw} from 'vue-router'
import Home from '../views/Home.vue'
import about from '../views/component/index.vue'
import Login from '../views/Login.vue'
import {url} from '../common/utils'
import loginIndex from '../views/login/index.vue'
import aboutChildIndex from '../views/about/child/index.vue'
import one from '../views/component/one.vue'
import two from '../views/component/two.vue'
import c from '../views/component/c.vue'
import {view} from '../views/view'
import noFound from '../views/404.vue'
import vant from '../vant/index.vue'
import vant1 from '../vant/newVue.vue'
const routes: Array<RouteRecordRaw> = [
  // {
  //   path:"/",
  //   redirect: 'deng'
  // },
  {
    path:"/",
    name:"view",
    component:view.shouye,

  },
  // {
  //   path:"/",
  //   name:"deng",
  //   component:view.deng
  // },
  {
    path: "/vant",
    name: "deng",
    component: vant
  },
  {
    path: "/vant1",
    name: "vant1",
    component: vant1
  },
  {
    path:"/zhu",
    name:"zhu",
    alias:"/c",
    component:view.zhu,
    meta:{
      isLogin:true    // 添加该字段，表示进入这个路由是需要登录的
    },//路由元
    children:[{
      path:"/zhu",
      name:"zhub",
      component:{
        template:`<div>123</div>`
      },
      meta:{
        user:"as"
      }
    },
    {
      path:"/zhu/a",
      name:"zhua",
      component:{
        template:`<div>123</div>`
      },
      meta:{
        userq:"aas"
      }
    }

  ],
    beforeEnter:function(to,from,next){
      // console.log(to)
      // console.log(from)
      next()
    }
  },
  {
    path:"/book",
    name:"book",
    component:view.book,
    beforeEnter:function(to,from,next){
     // var a=true
     var a=false;
      if(!a)
        next()
      else
        next({path:"/zhu"})
    },

  },
  {
    path:"/center",
    name:"center",
    component:view.center
  },
  {
    path:"/daohan",
    name:"daohan",
    component:view.daohan
  },
  {
    path:"/shop",
    name:"shop",
    component:view.shop
  },
  {
    path:"/backstage",
    name:"backstage",
    component:view.backstage,
    meta: {
      title:'首页'
    },
    children: [
      { path: '/acpetArrirce',name: 'acpetArrirce',component: view.acpetArrirce},
      { path: '/indexLeft',name: 'indexLeft',component: view.indexLeft}
    ]
  },
  {
    path:"/video",
    name:"video",
    component:view.video
  },
  {
    path:"/:id",
    redirect: "index"
  },
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home,
  //   children: [
  //     {
  //       path: '/',
  //       component: loginIndex,
  //       children: [
  //         {
  //           path: '/',
  //           component: aboutChildIndex
  //         }
  //       ]
  //     }
  //   ]
  // },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: about,
  //   children: [
  //     {
  //       path: '/about',
  //       component: one,
  //       children: [
  //         {
  //           path: '/about',
  //           component: two
  //         }
  //       ]
  //     },
  //     {
  //       path: '/e',
  //       component: c,
  //     },
  //   ]
  // },
  // {
  //   path: '/Login',
  //   name: 'Login',
  //   component: Login,
  // },
  // {
  //   path: '/formIndex',
  //   component: url('form/index')
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  linkActiveClass: 'active-class', // 自定义class 作为 路由样式
  linkExactActiveClass: 'init-class',
  scrollBehavior (to, from, savedPosition):any {
    console.log(to, from , savedPosition)
    // return 期望滚动到哪个的位置
    if(savedPosition) {
      return savedPosition;
    }else{
      return {x:0,y:0}
    }
  }
})
router.beforeEach((to,from,next)=>{
  console.log(to,from) // 增加路径 拦截
  var token = false
  // {
  //   path:"/",
  //   redirect: 'deng'
  // },
  // if (to.path !== '/deng' && token !==false) {
  //   next() // 如果token存在跳转非登录页 允许成功
  //   // {
  //   //   path:"/:id",
  //   //   redirect: "index"
  //   // }, 404页面 token存在 不是deng 不在路由路径 一律进这页面
  // } else if(to.path === '/deng') {
  //   next() // 是登录 允许进入 登录账号
  // } else { // 没有token 输入其他页面 一律进入登录页
  //   next({path:'deng'})
  // }
  
  next()
})
router.afterEach((to,from)=>{
  console.log(to,from)
})
router.addRoute({
  path: '/one',
  component: one
})

export default router
